package org.flexthinker.binding.bean.support
{
    /**
     * Helps writing a value into an object, having a path to a property.
     * the <code>propertyPath</code> can be in the following forms:
     * <ul>
     *  <li>propertyName</li>
     *  <li>propertyName.subProperty</li>
     *  <li>propertyName.subProperty1.subProperty2....subPropertyN</li>
     * </ul>
     */
    public class PropertyWriter
    {
        public function PropertyWriter()
        {
        }

        public static function setValue(source:Object, propertyPath:String, value:*):void
        {
            if( !source || !propertyPath || ( propertyPath && propertyPath.length == 0 ) )
            {
                return;
            }

            var subpaths:Array = propertyPath.split( "." );
            var subpathHost:Object = source;
            var lastProp:String = subpaths.pop();
            try
            {
                for each ( var subpath:String in subpaths )
                {
                    subpathHost = subpathHost[subpath];
                }
                subpathHost[lastProp] = value;
            }
            catch ( err:Error )
            {
                throw new PropertyResolutionError( err );
            }


        }
    }
}